package personal.vivian.interf;

import org.springframework.util.StringUtils;
import personal.vivian.annontation.DataField;
import personal.vivian.logic.DataContext;
import personal.vivian.logic.TemplatItem;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

public class RangeValidator implements DataValdator{
    @Override
    public void valdtor(DataContext dataContext, Object val, TemplatItem item) throws Exception {
        Field field=item.getTargetField();
        field.setAccessible(true);
        DataField annotation = field.getAnnotation(DataField.class);
        String value=annotation.val();
        Object o = field.get(val);
        String parseStr="";
        if(o instanceof String){
            parseStr=(String)o;
        }
        if(StringUtils.isEmpty(value)){
            return;
        }
        String[] split = value.split(";");
        List<String> splitList= Arrays.asList(split);
        if(!splitList.contains(parseStr)){
            throw new IllegalArgumentException(o.toString()+"不在允许范围内:"+value);
        }
    }
}
